package command 
{
	import cn.inswf.framework.mvcs.Command;
	import data.Chapter;
	import data.Lesson;
	import data.LMS;
	import event.ActionEvent;
	import event.AiccEvent;
	import event.LessonEvent;
	import event.PlayerEvent;
	import flash.display.DisplayObjectContainer;
	import service.JsService;
	import service.LocalSaveService;
	import service.MusicService;
	import util.JsUtil;
	import view.SelectView;
	
	/**
	 * ...
	 * @author lee
	 */
	public class ActionCommand extends Command 
	{
		[Inject]public var _event:ActionEvent;
		[Inject]public var _container:DisplayObjectContainer;
		[Inject]public var jsService:JsService;
		[Inject]public var localService:LocalSaveService;
		[Inject]public var music:MusicService;
		[Inject]public var select:SelectView;
		public function ActionCommand() 
		{
			
		}
		override public function execute():void 
		{
			super.execute();
			var str:String = _event.action;
			var list:Array = str.split(":");
			var act:String = list[0];
			var lesson:Lesson;
			if (act == "jump") {
				State.chapter = int(list[1]);
				State.lesson = int(list[2]);
				dispatchEvent(new LessonEvent(LessonEvent.CHANGE, State.chapter, State.lesson));
				dispatchEvent(new PlayerEvent(PlayerEvent.HIDE_MENU));
			}else if (act == "player") {
				var playerCode:String = list[1];
				if (playerCode == "infoend") {
					//片头完开始读取记录信息
					_container.addChild(getInstance(SelectView));
					LMS.decodeScorm(localService.getData());
					jsService.getInitData();
				}else if (playerCode == "start") {
					//直接从头开始播放
					dispatchEvent(new PlayerEvent(PlayerEvent.MEDIAPLAYER_START));
					music.load(Config.getParam("mp3"));
				}else if (playerCode == "continue") {
					//继续上次位置播放
					lesson = Chapter.getLessonById(LMS.location);
					State.chapter = lesson.chapter;
					State.lesson = lesson.lessonId;
					dispatchEvent(new PlayerEvent(PlayerEvent.MEDIAPLAYER_START));
					music.load(Config.getParam("mp3"));
				}
				else if (playerCode == "select") {
					//开始选择位置
					State.chapter = State.lesson = 0;
					if (LMS.location > 0) {
						select.start();
					}else {
						dispatchEvent(new PlayerEvent(PlayerEvent.MEDIAPLAYER_START));
						music.load(Config.getParam("mp3"));
					}
				}
			}else if (act == "lesson") {
				//课程
				var lessonCode:String = list[1];
				if (lessonCode == "restart") {
					//重新开始
					dispatchEvent(new LessonEvent(LessonEvent.CHANGE, State.chapter, State.lesson));
					dispatchEvent(new PlayerEvent(PlayerEvent.HIDE_MENU));
				}else if (lessonCode == "complete") {
					//结束课程
					lesson = Chapter.getLessonByFlag(State.chapter, State.lesson);
					if (lesson != null) {
						if (lesson.notMovie) {
							var score:int = list[2];
							lesson.score = score;
							if (score >= LMS.mastery_score) {
								lesson.complete();
							}
							dispatchEvent(new AiccEvent(AiccEvent.SAVE));
						}
						
					}
					
				}
			}
		}
	}

}